package com.edmond.jyframe.fragments;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.edmond.jyframe.R;
import com.edmond.jyview.widget.JYNestedScrollWebView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Created by edmond on 17-6-17.
 */

public class GoodsWebFragment extends Fragment {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_goods_web,container,false);

        JYNestedScrollWebView webView = (JYNestedScrollWebView)view.findViewById(R.id.web_goods);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setVerticalScrollBarEnabled(true);
        webView.setNestedScrollingEnabled(true);
        webView.loadDataWithBaseURL(null,getNewContent("<html><body>dfjksdjfklsdfj施展<img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/><img src=\"http://img05.tooopen.com/images/20150202/sy_80219211654.jpg\"/></body></html>"),"text/html","charset=UTF-8", null);

        return view;
    }

    private String getNewContent(String htmltext){

        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }

        Log.d("VACK", doc.toString());
        return doc.toString();
    }
}
